package com.example.xiaoxueqixiangmu.jiemian;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.example.xiaoxueqixiangmu.R;
import com.example.xiaoxueqixiangmu.bean.Order;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

public class chepiaoyuding4Activity extends AppCompatActivity {

    private TextView txtDetail , txtPay , txtNoPay;
    private ListView lv;
    private MyPaAdapter myPaAdapter;
    private ArrayList<HashMap<String,String>> dataArray;
    private static final String TAG = "chepiaoyuding4Activity";
    private Order order;
    private ArrayList<String> paArray , numberArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chepiaoyuding4);

        // Layout中的组件初始化
        txtDetail = findViewById(R.id.txtDetail);
        lv = findViewById(R.id.lv);
        dataArray = new ArrayList<>();
        txtPay = findViewById(R.id.txtPay);
        txtNoPay = findViewById(R.id.txtNoPay);

        // 从上一个Activity中获取到订单信息的数据，加载到组件中
        numberArray = new ArrayList<>();
        Intent intent = getIntent();
         paArray = intent.getStringArrayListExtra("paArray");
         order = (Order)intent.getSerializableExtra("order");
        final String orderId = "2021Y3t";

        // 解析订单内的数据到HashMap，再放入数据源
        for (int i = 0; i < paArray.size(); i++) {
            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("paName",paArray.get(i));
            hashMap.put("trainNo",order.getTrain().getTrainNo());
            hashMap.put("startDate",order.getTrain().getStartTime());
            String number = "6车"+(50+i)+"号";
            numberArray.add(number);
            hashMap.put("txt",number);
            dataArray.add(hashMap);
        }

        txtDetail.setText("订单提交成功，您的订单编号为："+orderId);

        //生成适配器，让ListView展示数据
        myPaAdapter = new MyPaAdapter(chepiaoyuding4Activity.this,dataArray);
        lv.setAdapter(myPaAdapter);

        // 确认支付按钮的监听事件，点击确认支付。将订单数据传入到下一个Activity中，并进行页面跳转
        txtPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent intents = new Intent(chepiaoyuding4Activity.this,chepiaoyuding5Activity.class);
                    intents.putExtra("orderId",orderId);
                    intents.putExtra("paArray",chepiaoyuding4Activity.this.paArray);
                    intents.putExtra("numberArray",chepiaoyuding4Activity.this.numberArray);
                    intents.putExtra("order",order);
                    startActivity(intents);
            }
        });

        // 暂不支付的监听事件，点击暂不支付，在订单页面中加入未支付订单
        txtNoPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();

            }
        });

        // 显示ActionBar上的返回键
        ActionBar supportActionBar = getSupportActionBar();
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }


    // 返回键的点击事件，销毁当前页面
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch ( item.getItemId() ){
            case android.R.id.home:
                chepiaoyuding4Activity.this.finish();
                break;
        }
        return true;
    }
}